package com.itheima.health.mq;

import com.itheima.health.utils.sms.SMSUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class SendMessage {
    @RabbitListener(queues = "health-queue")
    public void receive(Map map, Channel channel, Message message) throws IOException {
        //会话唯一ID
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String telephone = (String) map.get("telephone");
            String code = (String) map.get("code");
            System.out.println(telephone + " : " + code);
            // 调用阿里云 发送验证码短信
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, code);
            channel.basicAck(deliveryTag, false);//  手动处理ack  通知broker 删除当前消息
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);//  第三个参数 ： true  表示 出现代码业务一次 消息 重新放回队列中 再次获取
        }
    }
}
